<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let params = {
        chinese: "",
        math: "des",
        english: "des",
      };
      let arr2 = [
        {
          key: "1",
          name: "John Brown",
          chinese: 98,
          math: 60,
          english: 70,
        },
        {
          key: "2",
          name: "Jim Green",
          chinese: 98,
          math: 66,
          english: 89,
        },
        {
          key: "3",
          name: "Joe Black",
          chinese: 98,
          math: 90,
          english: 70,
        },
        {
          key: "4",
          name: "Jim Red",
          chinese: 88,
          math: 99,
          english: 89,
        },
      ];
      var compare = function (name, minor) {
        console.log(name);
        return function (o, p) {
          var a, b;
          if (o && p && typeof o === "object" && typeof p === "object") {
            a = o[name];
            b = p[name];

            if (a === b || params[name] === "") {
              return typeof minor === "function" ? minor(o, p) : 0; //如果年份相同，比较月份，月份相同比较日
            }
            if (params[name] === "des") {
              return b - a;
            } else if (params[name] === "asc") {
              return a - b;
            }
          } else {
            throw "字段有误";
          }
        };
      };
      let compare2 = function (name, index) {
        return function (o, p) {
          console.log(name);
          var a, b;
          if (o && p && typeof o === "object" && typeof p === "object") {
            a = o[name];
            b = p[name];

            if (a === b || params[name] === "") {
              console.log(
                index,
                Object.keys(params),
                Object.keys(params)[index + 1]
              );
              console.log(index);
              if (index >= Object.keys(params).length - 1) {
                return 0;
              } else {
                return compare2(Object.keys(params)[index + 1], index + 1)(
                  o,
                  p
                );
              }
            }
            if (params[name] === "des") {
              return b - a;
            } else if (params[name] === "asc") {
              return a - b;
            }
          } else {
            throw "字段有误";
          }
        };
      };
      // var resulr = arr2.sort(
      //   compare("chinese", compare("math", compare("english")))
      // );
      arr2.sort(compare2(Object.keys(params)[0], 0));
      console.log(arr2);
    </script>
  </body>
</html>
